package com.store.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.R;
import com.store.app.activity.HPTOpenDetailActivity;
import com.store.app.adapter.TuanCreatAdapter;
import com.store.app.bean.HPTOpenBean;
import com.store.app.dialog.AlertTipsDialog;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.widget.AutoListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author lushan
 * on 2017/3/23 0023 16:10
 * 火拼团
 */

public class TheOrderHuoPinTuanFragment extends Fragment implements IOnHttpListener {

    private View v;
    private int pageNum = 1;
    private int pageSize = 15;
    private AutoListView lv;
    //无网络页面
    private LinearLayout lin_loadfail;
    private ImageView img_fail_nonet;
    private ImageView img_fail_notext;
    private TextView tv_fail;
    private NetRequest netRequest;
    private TuanCreatAdapter adapter;
    private List<HPTOpenBean.ListBean> mList = new ArrayList<>();
    private Map<String, String> urlMap = new HashMap<>();
    private AlertTipsDialog mAlertTipsDialog;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_huopingtuan, container, false);
        findViews();
        setListeners();
        return v;
    }

    private void findViews(){
        netRequest = new NetRequest(this, getActivity());
//		lin_loadfail = (LinearLayout) v.findViewById(R.id.loadfail);
        img_fail_nonet = (ImageView) v.findViewById(R.id.loadfail_nonet);
        img_fail_notext = (ImageView) v.findViewById(R.id.loadfail_notext);
        tv_fail = (TextView) v.findViewById(R.id.loadfail_tv);
        lv = (AutoListView) v.findViewById(R.id.lv);
        adapter = new TuanCreatAdapter(getActivity(),mList,urlMap);
        lv.setAdapter(adapter);
        lv.setDivider(null);
    }


    private void setListeners(){
        adapter.setOnClickOrder(new TuanCreatAdapter.OnClickOrder() {
            @Override
            public void confirmClick(final HPTOpenBean.ListBean bean) {
                mAlertTipsDialog = new AlertTipsDialog(getActivity(), R.style.dialog_with_alpha, new AlertTipsDialog.AlertDialogListener() {
                    @Override
                    public void onConfirmClick() {
                            mAlertTipsDialog.dismiss();
                        netRequest.getHptconfirmOrder(3, bean.getActivity_id());

                    }
                    @Override
                    public void onCancelClick() {
                        mAlertTipsDialog.dismiss();
                    }
                });
                mAlertTipsDialog.show();
                mAlertTipsDialog.showCancleBtn(true);
                mAlertTipsDialog.setDialogTitle("提示");
                mAlertTipsDialog.setDialogContent("是否确认收货？");
            }

            @Override
            public void lockorderClick(HPTOpenBean.ListBean bean) {
//                Intent intent = new Intent(getActivity(),TuanCreateDetailActivity.class);
                Intent intent = new Intent(getActivity(),HPTOpenDetailActivity.class);
                intent.putExtra("activity_id",bean.getActivity_id());
                startActivity(intent);
            }
        });

        lv.setOnRefreshListener(new AutoListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNum = 1;
                netRequest.getHptOrder(1, pageNum, pageSize);
            }
        });
        lv.setOnLoadListener(new AutoListView.OnLoadListener() {
            @Override
            public void onLoad() {
                pageNum++;
                netRequest.getHptOrder(2, pageNum, pageSize);
            }
        });

        lv.onRefresh();
    }

    @Override
    public void onAfinalFail() {

    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if(requestId == 1){//刷新
            Log.i("mylog","aa "+result);
            HPTOpenBean bean = new Gson().fromJson(result,HPTOpenBean.class);
            mList.clear();
            lv.onRefreshComplete();
            mList.addAll(bean.getList());
            urlMap.clear();
            urlMap.putAll(bean.getDoc_url());
            lv.setResultSize(bean.getList().size());
            adapter.notifyDataSetChanged();
        }else if (requestId == 2){//加载更多
            HPTOpenBean bean = new Gson().fromJson(result,HPTOpenBean.class);
            lv.onLoadComplete();
            mList.addAll(bean.getList());
            urlMap.putAll(bean.getDoc_url());
            lv.setResultSize(bean.getList().size());
            adapter.notifyDataSetChanged();
        }else if (requestId == 3){//确认收货成功
            lv.onRefresh();
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {

    }
}
